package OSdesign.Function_show;

import com.sun.org.apache.xerces.internal.impl.xpath.regex.Match;

import java.io.File;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class OutputManager {
    public void printCheck(){
        File fcheck = new File("C:\\Users\\伊萧偌\\Desktop\\check.csv");
        String str = "";
        List<String> list = new ArrayList<>();
        Object[][] objects = new MatchManager().autoAllot_100();
        for(int i = 0;i<objects.length;i++){
            for(int j=0;j<4;j++){
                str+=objects[i][j];
                str+="    ";
            }
            str+="\n";
            list.add(str);
            str="";
        }
        CSVUtil out = new CSVUtil();
        out.exportCsv(fcheck,list);
        for(int i =0;i<objects.length;i++){
            for(int j=0;j<objects[0].length;j++){
                System.out.println(objects[i][j]);
            }
        }
    }

    public void printGrade(){
        File fgrade = new File("C:\\Users\\伊萧偌\\Desktop\\grade.csv");
        String str = "";
        List<String> list = new ArrayList<>();
        Object[][] objects = new InquireManager().gradeInquire();
        for(int i = 0;i<objects.length;i++){
            for(int j=0;j<6;j++){
                str+=objects[i][j];
                str+="    ";
            }
            str+="\n";
            list.add(str);
            str="";
        }
        CSVUtil out = new CSVUtil();
        out.exportCsv(fgrade,list);
    }

    public void printMedal(){
        File fmedal = new File("C:\\Users\\伊萧偌\\Desktop\\medal.csv");
        String str = "";
        List<String> list = new ArrayList<>();
        Object[][] objects = new OutputManager().medalsTotal();
        for(int i = 0;i<objects.length;i++){
            for(int j=0;j<6;j++){
                str+=objects[i][j];
                str+="    ";
            }
            str+="\n";
            list.add(str);
            str="";
        }
        CSVUtil out = new CSVUtil();
        out.exportCsv(fmedal,list);
    }

    public void printRecord(){
        File frecord = new File("C:\\Users\\伊萧偌\\Desktop\\record.csv");
        String str = "";
        List<String> list = new ArrayList<>();
        Object[][] objects = new InquireManager().breakRecordInquire();
        for(int i = 0;i<objects.length;i++){
            for(int j=0;j<5;j++){
                str+=objects[i][j];
                str+="    ";
            }
            str+="\n";
            list.add(str);
            str="";
        }
        CSVUtil out = new CSVUtil();
        out.exportCsv(frecord,list);
    }
    public Object[][] medalsTotal(){
        int i = 0,count = 0,rankng = 1;
        MySQL_Conn mysql = new MySQL_Conn();
        mysql.Connect();
        String sql = "select * from prize order by gold desc";
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try{
            while(true){
                if(!mysql.set.next())  break;
                count++;
            }
        }catch(SQLException sqle){
            sqle.printStackTrace();
        }
        Object[][] objects= new Object[count][6];
        System.out.println(count);
        try{
            mysql.set = mysql.stmt.executeQuery(sql);
        } catch(SQLException sqle){
            sqle.printStackTrace();
        }
        try {
            while(true){
                if(!mysql.set.next()) break;
                System.out.println("正在逐条执行SQL语句");
                objects[i][0] = rankng+"";
                objects[i][1] = mysql.set.getString("team");
                objects[i][2] = mysql.set.getString("totals");
                objects[i][3] = mysql.set.getString("gold");
                objects[i][4] = mysql.set.getString("sliver");
                objects[i][5] = mysql.set.getString("copper");
                i++;
            }
            System.out.println("逐条执行完毕");
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return objects;
    }

    public static void main(String[] args){
        new OutputManager().printMedal();
        new OutputManager().printRecord();
    }
}
